package com.blog.controller.manage;

import com.blog.bean.StayWord;
import com.blog.constant.RoleConstant;
import com.blog.service.StayWordService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/stayWordManage")
public class StayWordManageController {

    @Resource
    private StayWordService stayWordService;

    /**
     * 更新留言
     * 状态1：更新了留言人信息
     * 状态2：回复了留言
     *
     * @param stayWord
     * @return
     */
    @RequestMapping("/updateStayWord")
    @RequiresRoles(RoleConstant.ROLE_ADMIN)
    @ResponseBody
    public Object updateStayWord(StayWord stayWord) {
        Map<Object, Object> map = new HashMap<>();
        if (stayWord.getReplyContent() != "") {
            stayWord.setReplyTime(new Timestamp(System.currentTimeMillis()));
            stayWord.setStayWordState(0);
        } else {
            stayWord.setReplyTime(null);
            stayWord.setStayWordState(1);
        }
        int count = stayWordService.updateByPrimaryKeySelective(stayWord);
        if (count > 0) {
            map.put("result", "success");
        }
        return map;
    }

    /**
     * 获得一条留言
     *
     * @param stayWordId
     * @return
     */
    @RequestMapping("/getStayWord")
    @RequiresRoles(RoleConstant.ROLE_ADMIN)
    @ResponseBody
    public Object getStayWord(@RequestParam(value = "stayWordId") Long stayWordId) {
        Map<Object, Object> map = new HashMap<>();
        StayWord st = new StayWord();
        st.setStayWordId(stayWordId);
        StayWord stayWord = stayWordService.getStayWord(st);
        if (stayWord != null) {
            map.put("stayWord", stayWord);
        }
        return map;
    }

    /**
     * 删除留言
     *
     * @param stayWordId
     * @return
     */
    @RequestMapping("/deleteStayWord")
    @RequiresRoles(RoleConstant.ROLE_ADMIN)
    @ResponseBody
    public Object deleteStayWord(@RequestParam(value = "stayWordId") Long stayWordId) {
        Map<Object, Object> map = new HashMap<>();
        int count = stayWordService.deleteByPrimaryKey(stayWordId);
        if (count > 0) {
            map.put("result", "success");
        }
        return map;
    }

    /**
     * 留言列表
     *
     * @param pn
     * @param model
     * @return
     */
    @RequestMapping("/stayWordList")
    @RequiresRoles(RoleConstant.ROLE_ADMIN)
    public String stayWordList(@RequestParam(value = "pn", defaultValue = "1") String pn, Model model) {
        int pageNo = 1;
        try {
            pageNo = Integer.valueOf(pn);
        } catch (Exception e) {

        }
        PageHelper.startPage(pageNo, 15);
        List<StayWord> stayWordList = stayWordService.selectStayWord(new StayWord());
        PageInfo<StayWord> pageInfo = new PageInfo<>(stayWordList);
        model.addAttribute("pageInfo", pageInfo);
        return "manage-stay-word";
    }
}
